#include "legendmodel.h"
#include <QImage>


LegendModel::LegendModel(const QStringList &legend, QObject *parent) :
    QAbstractListModel(parent), _legend(legend)
{
}
//-----------------------------------------------------------------------------

int LegendModel::rowCount(const QModelIndex &/*parent*/) const
{
    return _legend.size();
}
//-----------------------------------------------------------------------------

QVariant LegendModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (role == Qt::DecorationRole)
        return QImage(":/img/curve.png");


    if (role == Qt::DisplayRole || role == Qt::EditRole)
        return _legend.at(index.row());

    return QVariant();
}
//-----------------------------------------------------------------------------

bool LegendModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    if (role == Qt::EditRole) {
        _legend.replace(index.row(), value.toString());
        emit dataChanged(index, index);
        return true;
    }
    return false;
}
//-----------------------------------------------------------------------------

QVariant LegendModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal) {
        if (section == 0)
            return QString(tr("Curves"));
    }
    else
        return QString::number(section + 1);

    return QVariant();
}
//-----------------------------------------------------------------------------

Qt::ItemFlags LegendModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = QAbstractListModel::flags(index);
    return index.isValid() ? (flags | Qt::ItemIsEditable) : flags;
}
//-----------------------------------------------------------------------------

bool LegendModel::removeCurve(int row)
{
    if (row >= 0 && row < _legend.size()) {
        _legend.removeAt(row);
        reset();
        return true;
    }
    return false;
}
//-----------------------------------------------------------------------------

QStringList LegendModel::stringList() const
{
    return _legend;
}
//-----------------------------------------------------------------------------
